package cn.edw.swu.bc.todolist

import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList

class ListActivity : AppCompatActivity() {
    lateinit var dbHelper: DBTodoHelper
    lateinit var adapter: ToDoAdapter
    val TAG = "@@List"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)
        init()
    }

    fun init() {
        dbHelper = DBTodoHelper(this, "todo.db", 1)
        val recyclerView = findViewById<RecyclerView>(R.id.recycleView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = ToDoAdapter()
        adapter.setData(dbHelper.readInDb())
        recyclerView.adapter = adapter
        val btn_edit = findViewById<TextView>(R.id.btn_edit)
        btn_edit.setOnClickListener {
            val editText = findViewById<TextView>(R.id.editText)
            val text=editText.text.toString()
            if (!text.isEmpty()) {
                val id=dbHelper.insertInDb(text)
                val todo=Todo(text,System.currentTimeMillis())
                todo.id=id
                adapter.addItem(todo)
            } else
                Toast.makeText(this, "请输入日程内容", Toast.LENGTH_SHORT).show()
            editText.text = null
        }
    }


    inner class ToDoAdapter : RecyclerView.Adapter<ToDoViewHolder>() {
        val data = arrayListOf<Todo>()

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToDoViewHolder {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_todo, parent, false)
            return ToDoViewHolder(view).apply {
                id = view.findViewById(R.id.todoID)
                content = view.findViewById(R.id.content)
                btn_check = view.findViewById(R.id.checkTodo)
                btn_del = view.findViewById(R.id.deleteTodo)
            }
        }

        override fun onBindViewHolder(holder: ToDoViewHolder, position: Int) {
            holder.render(data[position])
        }

        override fun getItemCount(): Int {
            return data.size
        }

        fun setData(arr: ArrayList<Todo>) {
            data.clear()
            data.addAll(arr)
            notifyDataSetChanged()
        }

        fun addItem(todo:Todo) {
            data.add(0,todo)
            notifyItemInserted(0)
        }

    }

    inner class ToDoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var id: TextView? = null
        var content: TextView? = null

        var btn_del: TextView? = null
        var btn_check: TextView? = null

        fun render(todo: Todo) {
            id?.text = todo.id.toString()
            content?.text = todo.content

            btn_del?.setOnClickListener {
                AlertDialog.Builder(it.context)
                    .setTitle("请确认")
                    .setMessage("您确认要删除日程吗")
                    .setNegativeButton("取消") { d, w ->
                        Toast.makeText(it.context, "已取消", Toast.LENGTH_SHORT).show()
                    }
                    .setPositiveButton("确认") { d, w ->
                        Toast.makeText(it.context, "已删除", Toast.LENGTH_SHORT).show()
                        dbHelper.delInDb(id?.text.toString().toInt())
                        adapter.setData(dbHelper.readInDb())
                    }
                    .show()
            }
            btn_check?.setOnClickListener {
                AlertDialog.Builder(it.context)
                    .setTitle("请确认")
                    .setMessage("您已经完成该日程了吗")
                    .setNegativeButton("取消") { d, w ->
                        Toast.makeText(it.context, "已取消", Toast.LENGTH_SHORT).show()
                    }
                    .setPositiveButton("确认") { d, w ->
                        Toast.makeText(it.context, "恭喜您完成日程", Toast.LENGTH_SHORT).show()
                        dbHelper.delInDb(id?.text.toString().toInt())
                        adapter.setData(dbHelper.readInDb())
                    }
                    .show()
            }
        }
    }
}